package com.xcl.service.impl;

import com.xcl.mapper.XclConsignerMapper;
import com.xcl.mapper.XclWaybillMapper;
import com.xcl.service.XclConsignerServiceInter;
import com.zzg.entity.Consigner;
import com.zzg.entity.TmsRegion;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName XclConsignerServiceImpl
 * @Description 收货人服务层实现类
 * @Author: xiaochaolei
 * @date 2021/7/12 15:33
 * @Version 1. 0
 */
@Service
public class XclConsignerServiceImpl implements XclConsignerServiceInter {

    @Autowired
    XclConsignerMapper xclConsigner;
    @Autowired
    XclWaybillMapper xclWaybillMapper;

    @Override
    public int insertConsigner(Consigner consigner) {
        //设置id
        consigner.setConsignerId(UuidUtil.getUuid());
        //第一次发货数量未0
        consigner.setShippingtimes(0.0);
        //根据字符串分割
        String[] split = consigner.getRemark().split("-");


        //用于存储地区id
        String regionId="";
        //判断是否存在
        TmsRegion existConsignerArea = xclWaybillMapper.isExistConsignerArea(split[0]+split[1]+split[2]+split[3]);
        if (existConsignerArea==null){
            //实例化地区(做新增操作)
            TmsRegion tmsRegion=new TmsRegion();
            //设置id
            tmsRegion.setRegionId(UuidUtil.getUuid());
            //设置省
            tmsRegion.setProvince(split[0]);
            //设置市
            tmsRegion.setCity(split[1]);
            //设置区
            tmsRegion.setDistrict(split[2]);
            //设置详细地址
            tmsRegion.setDetailedAddress(split[0]+split[1]+split[2]+ split[3]);
            //得到id
            regionId=tmsRegion.getRegionId();
            //新增地区
            xclWaybillMapper.insertTmsRegion(tmsRegion);
        }else {
            //得到id
            regionId=existConsignerArea.getRegionId();
        }
        //设置地区id
        consigner.setRegionId(regionId);
        //设置备注地址
        consigner.setRemark(split[0]+split[1]+split[2]+ split[3]);

        return xclConsigner.insertConsigner(consigner);
    }

    @Override
    public Consigner getConsignerById(String id) {
        return xclConsigner.getConsignerById(id);
    }

    @Override
    public int updateConsignerByid(Consigner consigner) {
        //根据字符串分割
        String[] split = consigner.getRemark().split("-");
        //用于存储地区id
        String regionId="";
        //判断是否修改了地址
        if (split.length==1){
            //判断是否存在
            TmsRegion existConsignerArea = xclWaybillMapper.isExistConsignerArea(consigner.getRemark());
            //得到id
            regionId=existConsignerArea.getRegionId();
            //设置备注地址
            consigner.setRemark(consigner.getRemark());
        }else {
            //判断是否存在
            TmsRegion existConsignerArea = xclWaybillMapper.isExistConsignerArea(split[0]+split[1]+split[2]+split[3]);
            //设置备注地址
            consigner.setRemark(split[0]+split[1]+split[2]+ split[3]);
            if (existConsignerArea==null){
                //实例化地区(做新增操作)
                TmsRegion tmsRegion=new TmsRegion();
                //设置id
                tmsRegion.setRegionId(UuidUtil.getUuid());
                //设置省
                tmsRegion.setProvince(split[0]);
                //设置市
                tmsRegion.setCity(split[1]);
                //设置区
                tmsRegion.setDistrict(split[2]);
                //设置详细地址
                tmsRegion.setDetailedAddress(split[0]+split[1]+split[2]+ split[3]);
                //得到id
                regionId=tmsRegion.getRegionId();
                //新增地区
                xclWaybillMapper.insertTmsRegion(tmsRegion);
            }else {
                //得到id
                regionId=existConsignerArea.getRegionId();
            }
        }
        //设置地区id
        consigner.setRegionId(regionId);
        return xclConsigner.updateConsignerByid(consigner);
    }

    @Override
    public int deleteConsignerBy(String id) {
        return xclConsigner.deleteConsignerBy(id);
    }
}
